module.exports = (sequelize, DataTypes) => {
    const PartPurchase = sequelize.define(
        'PartPurchase',
        {
            id: {
                type: DataTypes.BIGINT.UNSIGNED,
                allowNull: false,
                autoIncrement: true,
                primaryKey: true
            },
            name: {
                type: DataTypes.STRING,
                allowNull: false,
                validate: {
                    notNull: {
                        msg: '入库事项不能为空'
                    }
                },
                comment: '入库事项'
            },
            count: {
                type: DataTypes.INTEGER,
                allowNull: false,
                validate: {
                    notNull: {
                        msg: '入库数量不能为空'
                    },
                    min: {
                        args: 1,
                        msg: '入库数量不能小于1'
                    }
                },
                comment: '入库数量'
            },
            date: {
                type: DataTypes.DATEONLY,
                allowNull: false,
                validate: {
                    notNull: {
                        msg: '入库日期不能为空'
                    }
                },
                comment: '入库日期'
            },
            part_id: {
                type: DataTypes.BIGINT.UNSIGNED,
                allowNull: false,
                references: {
                    model: 'Part',
                    key: 'id'
                },
                validate: {
                    notNull: {
                        msg: '配件不能为空'
                    }
                },
                comment: '所属配件'
            },
            store_id: {
                type: DataTypes.BIGINT.UNSIGNED,
                allowNull: false,
                references: {
                    model: 'Store',
                    key: 'id'
                },
                validate: {
                    notNull: {
                        msg: '店别不能为空'
                    }
                },
                comment: '所属店别'
            }
        },
        {
            tableName: 'part_purchase'
        }
    )

    PartPurchase.associate = function(models) {
        models.PartPurchase.belongsTo(models.Part, { foreignKey: 'part_id', targetKey: 'id' })
        models.PartPurchase.belongsTo(models.Store, { foreignKey: 'store_id', targetKey: 'id' })
    }

    return PartPurchase
}
